package elitech.vietnam.myfashion.extend;

import android.content.Intent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import elitech.vietnam.myfashion.CopyImageViewActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.entites.ProductDetail;
import elitech.vietnam.myfashion.view.RecommendGroupView;

public class CopyDetailAdapter extends ArrayAdapter<ProductDetail> {
	private MainActivity		mContext;
	// private HeaderDetailView mHeader;
	public RecommendGroupView	mRecommendGroupView;
	public boolean				isRecommend			= true;

	private int					fingerState			= FINGER_RELEASED;
	private final static int	FINGER_RELEASED		= 0;
	private final static int	FINGER_TOUCHED		= 1;
	private final static int	FINGER_DRAGGING		= 2;
	private final static int	FINGER_UNDEFINED	= 3;

	public CopyDetailAdapter(MainActivity context, int resource, int textViewResourceId, Product product) {
		super(context, resource, textViewResourceId, product.DetailList);
		this.mContext = context;
	}

	static class ViewHolder {
		WebView		image;
		TextView	txtContent;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row = convertView;
		final ProductDetail detail = getItem(position);
		final ViewHolder holder;

		if (row == null) {

			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.copyphotodetail, parent, false);
			holder = new ViewHolder();

			holder.image = (WebView) row.findViewById(R.id.detail_webDetail);
			holder.txtContent = (TextView) row.findViewById(R.id.detail_txtDetailContent);

			holder.image.getSettings().setUseWideViewPort(true);
			holder.image.getSettings().setLoadWithOverviewMode(true);
			holder.image.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 

			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();
		// holder.image.setImageResource(R.drawable.loading300);
		String str = Const.SERVER_IMAGE_URL + detail.Image;
		if (str.indexOf("post") != -1)
			str = str.replace("app/", "");
		if (str.indexOf("account") != -1)
			str = str.replace("app/", "");
		
		String html = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta name='viewport' content='width=device-width user-scalable=no'></head><body>"
						+ "<img src=\"" + str.replace("/app", "") + "\" width=\"99%\" /></body></html>";
		holder.image.loadDataWithBaseURL(Const.SERVER_IMAGE_URL, html, "text/html", "UTF-8", null);
		
//		holder.image.loadUrl(str.replace("app/", ""));

		if (detail.Content != null && !detail.Content.equals("")) {
			holder.txtContent.setText(detail.Content.replace("<br>", "\n"));
			holder.txtContent.setPadding(8, 8, 8, 8);
		} else
			holder.txtContent.setVisibility(View.GONE);

		holder.image.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if (fingerState == FINGER_RELEASED)
						fingerState = FINGER_TOUCHED;
					else
						fingerState = FINGER_UNDEFINED;
					break;
				case MotionEvent.ACTION_UP:
					if (fingerState != FINGER_DRAGGING) {
						fingerState = FINGER_RELEASED;
						Intent i = new Intent(mContext, CopyImageViewActivity.class);
						i.putExtra("Link", detail.Image);
						mContext.startActivity(i);
					} else if (fingerState == FINGER_DRAGGING)
						fingerState = FINGER_RELEASED;
					else
						fingerState = FINGER_UNDEFINED;
					break;
				case MotionEvent.ACTION_MOVE:
					if (fingerState == FINGER_TOUCHED || fingerState == FINGER_DRAGGING)
						fingerState = FINGER_DRAGGING;
					else
						fingerState = FINGER_UNDEFINED;
					break;
				default:
					fingerState = FINGER_UNDEFINED;
				}
				return false;
			}
		});
		return row;
	}
}
